package all.com.jinyongqunxiax.modules;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.randy.fundation.JxFunc;

import all.com.jinyongqunxiax.R;
import all.com.jinyongqunxiax.component.JxForm;
import all.com.jinyongqunxiax.component.JxFormHeader;
import all.com.jinyongqunxiax.component.JxFormItemImage;
import all.com.jinyongqunxiax.component.JxFormItemSwitch;
import all.com.jinyongqunxiax.component.JxFormItemText;
import all.com.jinyongqunxiax.databinding.ItemDetailActivityBinding;
import all.com.jinyongqunxiax.model.mod.ItemManager;
import all.com.jinyongqunxiax.model.mod.manager.Mod;
import all.com.jinyongqunxiax.model.mod.manager.ModManager;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import jy.game.components.item.JyItem;

public class ItemDetailActivity extends AppCompatActivity {

    Options                   options;
    ItemDetailActivityBinding binding;
    JxForm                    form = new JxForm();

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.item_detail_activity);

        options = new Options(getIntent());
        setTitle(options.item);

        Mod         mod     = ModManager.shared().get(options.mod);
        ItemManager manager = mod.itemManager;
        manager.load();
        JyItem item = manager.map.get(options.item);
        if (item == null) {
            return;
        }

        form.connect(this, binding.itemDetailRecyclerView);
        form.add(new JxFormHeader(0, "基本信息", null));

        String imagePath0 = mod.imagePath(item.pic);
        form.add(new JxFormItemImage(0, "图标", imagePath0));
        form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("价格").value( "" + item.price).hint("").value(), null));
        form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label("等级").value( "" + item.level).hint("").value(), null));
        form.add(new JxFormItemSwitch(0, "掉落", item.drop, null));

        form.add(new JxFormHeader(0, "天赋", null));

        JxFunc.of(item.talent.split("#")).ifPresent(new JxFunc.Action<String>() {
            @Override
            public void yield(@NonNull String talent) {
                String trimmed = talent.trim();
                if (!trimmed.isEmpty()) {
                    form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label(trimmed).value( "").hint("").value(), null));
                }
            }
        });

        form.add(new JxFormHeader(0, "要求", null));
        JxFunc.of(item.requires).ifPresent(require
                -> form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label(Mod.requireName(require.name)).value( require.argvsValue).hint("").value(), null)));

        form.add(new JxFormHeader(0, "效果", null));

        JxFunc.of(item.triggers).ifPresent(trigger
                -> form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label(Mod.requireName(trigger.name)).value( trigger.argvsValue).hint("").value(), null)));

        form.refresh();
    }

    public static class Options {
        String mod;
        String item;

        Options(Intent intent) {
            this.mod = intent.getStringExtra("mod");
            this.item = intent.getStringExtra("item");
        }

        static Intent intent(Context context, String mod, String item) {
            Intent intent = new Intent(context, ItemDetailActivity.class);
            intent.putExtra("mod", mod);
            intent.putExtra("item", item);
            return intent;
        }
    }
}
